package com.example.leon.demo.Activity;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.example.leon.demo.R;

import com.example.leon.demo.common.BaseActivity;

import java.io.IOException;
import android.os.Handler;
import java.util.logging.LogRecord;

/**
 * Created by wang.long on 2016/3/30.
 */
public class HomeActivity extends BaseActivity {

    private WebView homeView;

    private Handler mHandler = new Handler();

    @JavascriptInterface
    @Override
    protected void onCreate(Bundle saveInstanceState){
        super.onCreate(saveInstanceState);

        super.setContentView(R.layout.home_layout);

        homeView=(WebView) findViewById(R.id.home);


        //支持Javascript
        homeView.getSettings().setJavaScriptEnabled(true);
        //优先使用缓存
        homeView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        //中文的支持:
        homeView.getSettings().setDefaultTextEncodingName("GBK");



        homeView.setWebChromeClient(new WebChromeClient() {

            @Deprecated
            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
                return super.onJsAlert(view, url, message, result);
            }

        });


        //homeView.addJavascriptInterface(new DemoJavaScriptInterface(),"demo");


        homeView.loadUrl("file:///android_asset/visaindex.html");

        //homeView.loadUrl("http://m.ctrip.com/html5/");

    }

    final class DemoJavaScriptInterface {

        DemoJavaScriptInterface() {

        }

        /**
         * This is not called on the UI thread. Post a runnable to invoke
         * loadUrl on the UI thread.
         */
        public void clickOnAndroid() {
            mHandler.post(new Runnable() {
                public void run() {
                    homeView.loadUrl("javascript:wave()");
                }
            });

        }
    }



}
